//
//  SearchRectangleMode.m
//  dataGathering
//
//  Created by apple on 14/12/23.
//  Copyright (c) 2014年 shuili. All rights reserved.
//

#import "SearchRectangleMode.h"

@interface SearchRectangleMode ()
@property(nonatomic, retain)AGSPoint* beginPoint;

@end

@implementation SearchRectangleMode

-(instancetype)initWithMapview :(AGSMapView*)mapview
{
    return [super initWithMapview:mapview];
}

-(void)mapView:(AGSMapView *)mapView didTapAndHoldAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
{
    if([self.myPolygon numPoints] == 4){
        while([self.myPolygon numPoints] > 0){
            [self.myPolygon removePointOnRing:0 atIndex:0];
        }
        _beginPoint = mappoint;
        [self.myPolygon addPointToRing:mappoint];
        [self.myGraphic setGeometry:self.myPolygon];
    }
}

-(void)mapView:(AGSMapView *)mapView didMoveTapAndHoldAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
{
    NSLog(@"moving");
    
    if([self.myPolygon numPoints] == 0){
        self.myGraphic = [[AGSGraphic alloc] initWithGeometry:mappoint symbol:self.innerSymbol attributes:nil];
        [self.myGraphicsLayer addGraphic:self.myGraphic];
        _beginPoint = mappoint;
        [self.myPolygon addPointToRing:mappoint];
        return;
    }
    
    while([self.myPolygon numPoints] > 1){
        [self.myPolygon removePointOnRing:0 atIndex:([self.myPolygon numPoints]-1)];
    }
    
    AGSPoint *firstCornerPoint = [[AGSPoint alloc] initWithX:[mappoint x] y:[_beginPoint y] spatialReference:[mappoint spatialReference]];
    
    AGSPoint *secondCornerPoint = [[AGSPoint alloc] initWithX:[_beginPoint x] y:[mappoint y] spatialReference:[mappoint spatialReference]];
    
    [self.myPolygon addPointToRing:firstCornerPoint];
    [self.myPolygon addPointToRing:mappoint];
    [self.myPolygon addPointToRing:secondCornerPoint];
    [self.myPolygon closePolygon];
    
    [self.myGraphic setGeometry:self.myPolygon];

    
}

-(void)mapView:(AGSMapView *)mapView didEndTapAndHoldAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
{
    NSLog(@"end");
    
    while([self.myPolygon numPoints] > 1){
        [self.myPolygon removePointOnRing:0 atIndex:([self.myPolygon numPoints]-1)];
    }
    
    AGSPoint *firstCornerPoint = [[AGSPoint alloc] initWithX:[mappoint x] y:[_beginPoint y] spatialReference:[mappoint spatialReference]];
    
    AGSPoint *secondCornerPoint = [[AGSPoint alloc] initWithX:[_beginPoint x] y:[mappoint y] spatialReference:[mappoint spatialReference]];
    
    [self.myPolygon addPointToRing:firstCornerPoint];
    [self.myPolygon addPointToRing:mappoint];
    [self.myPolygon addPointToRing:secondCornerPoint];
    [self.myPolygon closePolygon];
    
    //[self stop];
    
    //if (self.delegate && [self.delegate respondsToSelector:@selector(SearchBaseMode:didFinishDrawing:)]) {
    //    [self.delegate SearchBaseMode:self didFinishDrawing:self.myPolygon];
    //}
}

-(void)mapView:(AGSMapView *)mapView didClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint features:(NSDictionary *)features
{
    if([[AGSGeometryEngine defaultGeometryEngine] geometry:mappoint intersectsGeometry:self.myPolygon])
    {
        [self stop];
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(SearchBaseMode:didFinishDrawing:)]) {
            [self.delegate SearchBaseMode:self didFinishDrawing:self.myPolygon];
        }
    }
    
}


@end
